package com.kgc.sportscircle.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kgc.sportscircle.dao.MallproductMapper;
import com.kgc.sportscircle.pojo.Mallcart;
import com.kgc.sportscircle.pojo.Mallproduct;

import com.kgc.sportscircle.redis.CartPrefix;
import com.kgc.sportscircle.redis.RedisService;
import com.kgc.sportscircle.service.MallcartService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
@Service(timeout = 50000,retries = 2,version = "1.11",interfaceClass=MallcartService.class)
public class MallCartServiceImpl implements MallcartService {
    @Resource
    private RedisService redisService;
    @Resource
    private MallproductMapper mallproductMapper;

    /**
     * 新增购物车
     * @param userId
     * @param sku 商品skuid
     * @param num
     * @return
     */
    public int addCart(String userId, String sku, int num) {
        Boolean exists = redisService.existsValue(CartPrefix.getCartList,userId,sku);
        if (exists){
            String json = redisService.hget(CartPrefix.getCartList,userId,sku);
            if (json!=null){
                Mallcart mallcart = JSON.toJavaObject(JSONObject.parseObject(json),Mallcart.class);
                //数量加一
                mallcart.setProductNum(mallcart.getProductNum()+num);
                //放入缓存
                redisService.hset(CartPrefix.getCartList,userId,sku,JSON.toJSONString(mallcart));
            }else{
                return 0;
            }
            return 1;
        }
        //根据商品id获取商品
        Mallproduct mallproduct = mallproductMapper.selectProductBySkuId(sku);
        if (mallproduct==null){
            return 0;
        }
        Mallcart mallcart = new Mallcart();
        mallcart.setProductId(sku);
        mallcart.setAttributes(mallproduct.getAttributes());
        mallcart.setProductName(mallproduct.getName());
        mallcart.setProductPrice(mallproduct.getPrice());
        mallcart.setIsDelete(mallproduct.getIsDelete());
        mallcart.setProductIcon(mallproduct.getExtension3());
        mallcart.setProductNum(num);
        redisService.hset(CartPrefix.getCartList,userId,sku,JSON.toJSONString(mallcart));
        return 1;
    }

    /**
     * 查询所有购物车
     * @param userId
     * @return
     */
    public List<Mallcart> getCartList(String userId) {

       List<String> jsonList = redisService.hvals(CartPrefix.getCartList,userId);
       List<Mallcart> cartList = new LinkedList<Mallcart>();
       for(String json : jsonList){
           Mallcart mallcart = JSON.parseObject(json,Mallcart.class);
           cartList.add(mallcart);
       }
        return cartList;
    }

    /**
     * 更新商品数量
     * @param userId
     * @param sku
     * @param num
     * @return
     */
    public int updateCartNum(String userId, String sku, int num) {
        String json = redisService.hget(CartPrefix.getCartList,userId,sku);
        if (json==null){
            return 0;
        }
        Mallcart mallcart = JSONObject.parseObject(json,Mallcart.class);
        if (num>0){
            mallcart.setProductNum(mallcart.getProductNum()+num);
        }else {
            if (mallcart.getProductNum()<=1){
                return 0;
            }
            mallcart.setProductNum(mallcart.getProductNum()+num);
        }
        redisService.hset(CartPrefix.getCartList,userId,sku,JSON.toJSONString(mallcart));
        return 1;
    }
    /**
     * 全选商品
     * @param userId
     * @param checked
     * @return
     */
    public int checkAll(String userId, String checked) {
        return 0;
    }

    /**
     * 删除商品
     * @param userId
     * @param sku
     * @return
     */
    public int delCartProduct(String userId, String sku) {
        redisService.hdel(CartPrefix.getCartList,userId,sku);
        return 1;
    }

    /**
     * 清空购物车
     * @param userId
     * @return
     */
    public int delCart(String userId) {
        return 0;
    }
}
